#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2021/12/14 12:48 
# @Author : cc
# @File : testpy.py


import unittest,threading,time
import threadpool

def test_task(name):
    print(f"{threading.current_thread().name}:",name)
    time.sleep(0.1)

class TestTask(unittest.TestCase):
    def test01(self):
        test_task(1)
    def test02(self):
        test_task(2)
    def test03(self):
        test_task(3)
    def test04(self):
        test_task(4)
    def test05(self):
        test_task(5)
    def test06(self):
        test_task(6)
    def test07(self):
        test_task(7)
    def test08(self):
        test_task(8)
    def test09(self):
        test_task(9)
    def test10(self):
        test_task(10)

def get_testcase():
    tl = unittest.TestLoader()
    suite =tl.loadTestsFromTestCase(TestTask)
    return suite

def action(suite):
    runner = unittest.TextTestRunner()
    result = runner.run(suite)
    return  result

if __name__ == '__main__':
    # t =unittest.TestLoader()
    # suite =t.discover("./", "test*.py")
    # runner =unittest.TextTestRunner()
    # runner.run(suite)
    start=time.time()
    pool = threadpool.ThreadPool(10)  # 启动10个线程运行
    suite = get_testcase()  # 获取测试用例
    # print("suite",type(suite),suite)
    requests = threadpool.makeRequests(action,suite)
    # print("requests",requests)
    # [pool.putRequest(req) for req in requests]
    # pool.wait()
    for req in requests:
        print("任务是:",req)
        pool.putRequest(req)
    pool.wait()  # 等待任务完成
    end = time.time()
    print("共花费的时间：", (end - start))